package com.erclab.internal.xpresso.webapp.servlets;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.security.User;

public class XpressoFCKConnector extends FCKEditorConnector {

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setBaseDir(request);
		super.doGet(request, response);		
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setBaseDir(request);
		super.doPost(request, response);		
	}

	private void setBaseDir(HttpServletRequest request) {
				
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		baseDir = Settings.getProperty("upload.path");
		
		String userDirPath = getServletContext().getRealPath(baseDir) + "/" + user.getIdUser();
		
		File currentDir = new File(userDirPath);
		if(!currentDir.exists()){
			currentDir.mkdir();
		}
		this.baseDir = Settings.getProperty("upload.path") + user.getIdUser()  + "/";
	}

}
